﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System;
/// <summary>
/// 可以供客户端和服务器一块使用
/// </summary>
public class TcpSocket
{
    //当前实例化的socket
    private Socket socket;
    //socket上的数据
    private byte[] data;
    //区分服务器  还是客户端
    private bool isServer;

    public bool isStart = false;
    public TcpSocket(Socket socket, int dataLength, bool isServer)
    {
        this.socket = socket;

        data = new byte[dataLength];

        this.isServer = isServer;

    }

    #region 接受
    public void ClientReceive()
    {
        //data 数据缓存 0:指的是 接受位的偏移量   data.length指的是数据的长度   SocketFlags.None固定格式 
        //new AsyncCallback(ClientEndReceive)需要有返回值的回调函数,返回值是下面的方法
        //public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state);

        socket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ClientEndReceive), null);
    }

    public Queue queue = new Queue();
    public void ClientEndReceive(IAsyncResult ar)
    {
        //数据的处理
        int receiveLength = socket.EndReceive(ar);    //将接受到的数据赋值给receiveLength
        //把接受完毕的字节数组转化为 string类型
        string dataStr = System.Text.Encoding.UTF8.GetString(data, 0, receiveLength);

        if (isServer)    //判断是否接收到了因为开始设定了isServer是布尔值
        {
            Debug.Log("服务器接受到了:" + dataStr);
            queue.Enqueue(dataStr);
            //服务器要回什么
            for (int i = 0; i < Server.Instance.clients.Count; i++)
            {
                if (Server.Instance.clients[i].ClientConnect())
                {
                    Server.Instance.clients[i].ClientSend("服务器回复:是的");

                    //Server.Instance.clients[i].ClientSeed(System.Text.Encoding.UTF8.GetBytes("服务器收到了客户端发送的:" + dataStr));
                }
            }
        }
        else
        {
            Debug.Log("客户端接受到了:" + dataStr);
            queue.Enqueue(dataStr);
        }
    }
    #endregion


    #region 发送
    public void ClientSend(string msg)     //这里发送的是byte的字节
    {
        byte[] data = System.Text.Encoding.UTF8.GetBytes(msg);
        socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ClientSendEnd), null);
    }

    private void ClientSendEnd(IAsyncResult ar)
    {
        socket.EndSend(ar);
    }

    #endregion

    #region 连接
    public void ClientConnect(string ip, int port)
    {
        socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(ClientEndConnect), null);

    }

    public void ClientEndConnect(IAsyncResult ar)
    {
        if (ar.IsCompleted)
        {
            Debug.Log("连接成功");
        }
        socket.EndConnect(ar);
    }
    #endregion

    #region 断开

    public bool ClientConnect()
    {
        return socket.Connected;
    }

    #endregion
}

